import 'dart:convert';
import 'dart:math';

import 'package:deepseek_desktop/components/Avatar.dart';
import 'package:deepseek_desktop/components/ModelController.dart';
import 'package:deepseek_desktop/controller/SettingController.dart';
import 'package:deepseek_desktop/entity/Model.dart';
import 'package:deepseek_desktop/utils/Util.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class SettingView extends StatelessWidget {
  Color iconColor = const Color.fromARGB(255, 127, 92, 180);
  Color textColor = const Color.fromARGB(255, 168, 126, 236);
  Color selectColor = const Color.fromARGB(255, 77, 107, 254);
  Color bgDarkColor = const Color.fromARGB(255, 54, 37, 79);

  Color bgColor = const Color.fromARGB(150, 64, 46, 88);

  Rx<String> selectMenu = '用户设置'.obs;

  SettingController controller = Get.put(SettingController());

  FocusNode _nickerFocusNode = FocusNode();
  TextEditingController _nicker_controller = TextEditingController();

  FocusNode _accessTokenFocusNode = FocusNode();
  TextEditingController _accessToken_controller = TextEditingController();

  FocusNode _baseUrlFocusNode = FocusNode();
  TextEditingController _baseUrl_controller = TextEditingController();

  FocusNode _searchUrlFocusNode = FocusNode();
  TextEditingController _searchUrlController = TextEditingController();

  FocusNode _bingSearchAccessTokenFocusNode = FocusNode();
  TextEditingController _bingSearchAccessToken_controller =
      TextEditingController();

  ModelController modelController = Get.put(ModelController());

  @override
  Widget build(BuildContext context) {
    _nicker_controller.text = controller.setting.value.profileSetting.nicker;
    _nickerFocusNode.addListener(() {
      controller.setting.update((val) {
        val!.profileSetting.nicker = _nicker_controller.text;
      });
      Util.writeFile('settings.json', jsonEncode(controller.setting.value));
    });

    _accessToken_controller.text =
        controller.setting.value.apiSetting.accessToken;
    _accessTokenFocusNode.addListener(() {
      controller.setting.update((val) {
        val!.apiSetting.accessToken = _accessToken_controller.text;
      });
      Util.writeFile('settings.json', jsonEncode(controller.setting.value));
    });

    _baseUrl_controller.text = controller.setting.value.apiSetting.baseUrl;
    _baseUrlFocusNode.addListener(() {
      controller.setting.update((val) {
        val!.apiSetting.baseUrl = _baseUrl_controller.text;
      });
      Util.writeFile('settings.json', jsonEncode(controller.setting.value));
    });

    _searchUrlController.text =
        controller.setting.value.apiSetting.bingSearchBaseUrl;
    _searchUrlFocusNode.addListener(() {
      controller.setting.update((val) {
        val!.apiSetting.bingSearchBaseUrl = _searchUrlController.text;
      });
      Util.writeFile('settings.json', jsonEncode(controller.setting.value));
    });

    _bingSearchAccessToken_controller.text =
        controller.setting.value.apiSetting.bingSearchAccessToken;
    _bingSearchAccessTokenFocusNode.addListener(() {
      controller.setting.update((val) {
        val!.apiSetting.bingSearchAccessToken =
            _bingSearchAccessToken_controller.text;
      });
      Util.writeFile('settings.json', jsonEncode(controller.setting.value));
    });

    return Row(
      children: [
        Container(
          width: 240,
          color: bgColor,
          child: Column(
            children: [
              Container(
                decoration: BoxDecoration(
                    border: Border(
                        bottom: BorderSide(
                  width: 1,
                  color: bgDarkColor,
                ))),
                child: Container(
                  padding: const EdgeInsets.all(10),
                  child: const SafeArea(
                    child: Row(
                      children: [
                        Text('设置', style: TextStyle(color: Colors.white)),
                      ],
                    ),
                  ),
                ),
              ),
              Expanded(
                child: ListView(
                  children: [
                    Obx(() => buildItem('用户设置', Icons.person)),
                    Obx(() => buildItem('API 设置', Icons.chat)),
                    Obx(() => buildItem('关于&帮助', Icons.help)),
                  ],
                ),
              ),
            ],
          ),
        ),
        Expanded(
          child: Container(
            color: Colors.white,
            child: Obx(() {
              if (selectMenu.value == '用户设置') {
                return Container(
                  padding: const EdgeInsets.all(20),
                  child: ListView(
                    children: [
                      const SizedBox(
                        height: 20,
                      ),
                      Center(
                        child: Stack(
                          children: [
                            MaterialButton(
                              onPressed: () {
                                Util.pickAndSaveImage().then((value) {
                                  controller.setting.update((val) {
                                    val!.profileSetting.avatar = value;
                                  });
                                  Util.writeFile('settings.json',
                                      jsonEncode(controller.setting.value));
                                });
                              },
                              // 外面禁止点击
                              shape: const CircleBorder(),
                              child: Avatar(
                                filePath: controller
                                    .setting.value.profileSetting.avatar,
                                size: 80,
                              ),
                            ),
                            Positioned(
                              bottom: 0,
                              right: 0,
                              child: Container(
                                padding: const EdgeInsets.all(5),
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(20),
                                ),
                                child: const Icon(
                                  Icons.camera_alt,
                                  color: Colors.grey,
                                  size: 20,
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                      const SizedBox(
                        height: 20,
                      ),
                      TextField(
                        decoration: InputDecoration(
                          hintText: '请输入您的昵称',
                          labelText: '昵称',
                          prefixIcon: Icon(Icons.person, color: iconColor),
                          helperText: '您的昵称将显示给聊天对象',
                          border: OutlineInputBorder(
                            borderSide: BorderSide(color: textColor, width: 1),
                            borderRadius: BorderRadius.circular(10),
                          ),
                        ),
                        controller: _nicker_controller,
                        focusNode: _nickerFocusNode,
                      ),
                    ],
                  ),
                );
              } else if (selectMenu.value == 'API 设置') {
                return Container(
                  padding: const EdgeInsets.only(top: 30),
                  child: ListView(
                    // 左对齐
                    children: [
                      // const SizedBox(height: 20,),
                      Container(
                        padding:
                            const EdgeInsets.only(left: 20, right: 20, top: 10),
                        child: Container(
                          // 阴影
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(10),
                            boxShadow: [
                              BoxShadow(
                                color: Colors.grey.withOpacity(0.5),
                                offset: const Offset(0.0, 1.0), //阴影xy轴偏移量
                                blurRadius: 8.0, //阴影模糊程度
                                spreadRadius: 0.0, //阴影扩散程度
                              )
                            ],
                          ),
                          padding: const EdgeInsets.all(20),
                          child: Column(
                            // 左对齐
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                "模型 API 设置",
                                style:
                                    TextStyle(color: textColor, fontSize: 20),
                              ),
                              const SizedBox(
                                height: 10,
                              ),
                              DropdownButtonFormField(
                                decoration: InputDecoration(
                                  labelText: '选择模型',
                                  prefixIcon: Icon(Icons.web, color: iconColor),
                                  helperText: '选择模型来连接聊天服务',
                                  border: OutlineInputBorder(
                                    borderSide:
                                        BorderSide(color: textColor, width: 1),
                                    borderRadius: BorderRadius.circular(10),
                                  ),
                                ),
                                selectedItemBuilder: (BuildContext context) {
                                  return ['DeepSeek', 'Custom']
                                      .map((String value) {
                                    return Text(value);
                                  }).toList();
                                },
                                alignment: Alignment.topLeft,
                                value:
                                    controller.setting.value.apiSetting.isCustom
                                        ? 'Custom'
                                        : 'DeepSeek',
                                items: const [
                                  DropdownMenuItem(
                                      value: 'DeepSeek',
                                      child: Column(
                                        mainAxisAlignment:
                                            MainAxisAlignment.start,
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: [
                                          Text('DeepSeek'),
                                          Text(
                                            'https://api.deepseek.com/',
                                            style:
                                                TextStyle(color: Colors.grey),
                                          ),
                                        ],
                                      )),
                                  DropdownMenuItem(
                                      value: 'Custom',
                                      child: Column(
                                        mainAxisAlignment:
                                            MainAxisAlignment.start,
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: [
                                          Text('自定义服务器地址'),
                                          Text(
                                            '请输入自定义服务器地址',
                                            style:
                                                TextStyle(color: Colors.grey),
                                          ),
                                        ],
                                      )),
                                ],
                                onChanged: (value) {
                                  controller.setting.update((val) {
                                    val!.apiSetting.isCustom =
                                        value == 'Custom';
                                  });
                                  Util.writeFile('settings.json',
                                      jsonEncode(controller.setting.value));
                                },
                              ),
                              if (controller.setting.value.apiSetting.isCustom)
                                Column(
                                  children: [
                                    SizedBox(
                                      height: 20,
                                    ),
                                    TextField(
                                      decoration: InputDecoration(
                                        disabledBorder: OutlineInputBorder(
                                          borderSide: BorderSide(
                                              color: textColor, width: 1),
                                          borderRadius:
                                              BorderRadius.circular(10),
                                        ),
                                        hintText: '请输入自定义 API 基础 URL',
                                        labelText: '自定义 API 基础 URL',
                                        prefixIcon:
                                            Icon(Icons.key, color: iconColor),
                                        helperText: '您的 API 基础 URL 将用于访问聊天服务',
                                        border: OutlineInputBorder(
                                          borderSide: BorderSide(
                                              color: textColor, width: 1),
                                          borderRadius:
                                              BorderRadius.circular(10),
                                        ),
                                      ),
                                      controller: _baseUrl_controller,
                                      focusNode: _baseUrlFocusNode,
                                    ),
                                  ],
                                ),
                              const SizedBox(
                                height: 20,
                              ),
                              TextField(
                                decoration: InputDecoration(
                                  hintText: '请输入您的访问令牌',
                                  labelText: 'API Key',
                                  prefixIcon: Icon(Icons.key, color: iconColor),
                                  helperText: '您的 API 访问令牌将用于访问聊天服务',
                                  border: OutlineInputBorder(
                                    borderSide:
                                        BorderSide(color: textColor, width: 1),
                                    borderRadius: BorderRadius.circular(10),
                                  ),
                                ),
                                controller: _accessToken_controller,
                                focusNode: _accessTokenFocusNode,
                              ),
                            ],
                          ),
                        ),
                      ),
                      const SizedBox(
                        height: 5,
                      ),
                      // Container(
                      //   padding:
                      //       const EdgeInsets.only(left: 20, right: 20, top: 10),
                      //   child: Container(
                      //     // 阴影
                      //     decoration: BoxDecoration(
                      //       color: Colors.white,
                      //       borderRadius: BorderRadius.circular(10),
                      //       boxShadow: [
                      //         BoxShadow(
                      //           color: Colors.grey.withOpacity(0.5),
                      //           offset: const Offset(0.0, 1.0), //阴影xy轴偏移量
                      //           blurRadius: 8.0, //阴影模糊程度
                      //           spreadRadius: 0.0, //阴影扩散程度
                      //         )
                      //       ],
                      //     ),
                      //     padding: const EdgeInsets.all(20),
                      //     child: Column(
                      //       // 左对齐
                      //       crossAxisAlignment: CrossAxisAlignment.start,
                      //       children: [
                      //         Text(
                      //           "Bing-Search Plugin Settings",
                      //           style:
                      //               TextStyle(color: textColor, fontSize: 20),
                      //         ),
                      //         SizedBox(
                      //           height: 10,
                      //         ),
                      //         DropdownButtonFormField(
                      //           decoration: InputDecoration(
                      //             labelText: 'Select a Bing-Search server',
                      //             prefixIcon: Icon(Icons.web, color: iconColor),
                      //             helperText:
                      //                 'Select a server to connect to the Bing-Search service',
                      //             border: OutlineInputBorder(
                      //               borderSide:
                      //                   BorderSide(color: textColor, width: 1),
                      //               borderRadius: BorderRadius.circular(10),
                      //             ),
                      //           ),
                      //           selectedItemBuilder: (BuildContext context) {
                      //             return ['Azure', 'Custom']
                      //                 .map((String value) {
                      //               return Text(value);
                      //             }).toList();
                      //           },
                      //           alignment: Alignment.topLeft,
                      //           value: controller.setting.value.apiSetting
                      //                   .bingSearchIsCustom
                      //               ? 'Custom'
                      //               : 'Azure',
                      //           items: const [
                      //             DropdownMenuItem(
                      //                 value: 'Azure',
                      //                 child: Column(
                      //                   mainAxisAlignment:
                      //                       MainAxisAlignment.start,
                      //                   crossAxisAlignment:
                      //                       CrossAxisAlignment.start,
                      //                   children: [
                      //                     Text('Azure'),
                      //                     Text(
                      //                       'https://api.bing.microsoft.com',
                      //                       style:
                      //                           TextStyle(color: Colors.grey),
                      //                     ),
                      //                   ],
                      //                 )),
                      //             DropdownMenuItem(
                      //                 value: 'Custom',
                      //                 child: Column(
                      //                   mainAxisAlignment:
                      //                       MainAxisAlignment.start,
                      //                   crossAxisAlignment:
                      //                       CrossAxisAlignment.start,
                      //                   children: [
                      //                     Text('Custom'),
                      //                     Text(
                      //                       'Please enter your custom server address',
                      //                       style:
                      //                           TextStyle(color: Colors.grey),
                      //                     ),
                      //                   ],
                      //                 )),
                      //           ],
                      //           onChanged: (value) {
                      //             controller.setting.update((val) {
                      //               val!.apiSetting.bingSearchIsCustom =
                      //                   value == 'Custom';
                      //             });
                      //             Util.writeFile('settings.json',
                      //                 jsonEncode(controller.setting.value));
                      //           },
                      //         ),
                      //         if (controller
                      //             .setting.value.apiSetting.bingSearchIsCustom)
                      //           Column(
                      //             children: [
                      //               SizedBox(
                      //                 height: 20,
                      //               ),
                      //               TextField(
                      //                 decoration: InputDecoration(
                      //                   disabledBorder: OutlineInputBorder(
                      //                     borderSide: BorderSide(
                      //                         color: textColor, width: 1),
                      //                     borderRadius:
                      //                         BorderRadius.circular(10),
                      //                   ),
                      //                   hintText:
                      //                       'Please enter your Bing-Search API base URL',
                      //                   labelText: 'Bing-Search API base URL',
                      //                   prefixIcon:
                      //                       Icon(Icons.key, color: iconColor),
                      //                   helperText:
                      //                       'Your API base URL will be used to access the Bing-Search service.',
                      //                   border: OutlineInputBorder(
                      //                     borderSide: BorderSide(
                      //                         color: textColor, width: 1),
                      //                     borderRadius:
                      //                         BorderRadius.circular(10),
                      //                   ),
                      //                 ),
                      //                 controller: _searchUrlController,
                      //                 focusNode: _searchUrlFocusNode,
                      //               ),
                      //             ],
                      //           ),
                      //         const SizedBox(
                      //           height: 20,
                      //         ),
                      //         TextField(
                      //           decoration: InputDecoration(
                      //             hintText: 'Please enter your AccessToken',
                      //             labelText: 'Bing-Search AccessToken',
                      //             prefixIcon: Icon(Icons.key, color: iconColor),
                      //             helperText:
                      //                 'Your AccessToken will be used to access the Bing-Search service.',
                      //             border: OutlineInputBorder(
                      //               borderSide:
                      //                   BorderSide(color: textColor, width: 1),
                      //               borderRadius: BorderRadius.circular(10),
                      //             ),
                      //           ),
                      //           controller: _bingSearchAccessToken_controller,
                      //           focusNode: _bingSearchAccessTokenFocusNode,
                      //         ),
                      //       ],
                      //     ),
                      //   ),
                      // ),
                      const SizedBox(
                        height: 5,
                      ),
                      Container(
                        padding:
                            const EdgeInsets.only(left: 20, right: 20, top: 10),
                        child: Container(
                          // 阴影
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(10),
                            boxShadow: [
                              BoxShadow(
                                color: Colors.grey.withOpacity(0.5),
                                offset: const Offset(0.0, 1.0), //阴影xy轴偏移量
                                blurRadius: 8.0, //阴影模糊程度
                                spreadRadius: 0.0, //阴影扩散程度
                              )
                            ],
                          ),
                          padding: const EdgeInsets.all(20),
                          child: Column(
                            // 左对齐
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                "自定义模型设置",
                                style:
                                    TextStyle(color: textColor, fontSize: 20),
                              ),
                              const SizedBox(
                                height: 10,
                              ),
                              SizedBox(
                                width: double.infinity,
                                child: Obx(() => DataTable(
                                      columns: const [
                                        DataColumn(label: Text('模型显示名称')),
                                        DataColumn(label: Text('模型名称')),
                                        DataColumn(label: Text('操作')),
                                      ],
                                      rows: [
                                        for (var i = 0;
                                            i < modelController.items.length;
                                            i++)
                                          DataRow(cells: [
                                            DataCell(Text(modelController
                                                .items[i].displayName)),
                                            DataCell(Text(
                                                modelController.items[i].name)),
                                            DataCell(Row(
                                              children: [
                                                IconButton(
                                                  onPressed: () {
                                                    // 弹出对话框，输入模型显示名称和模型名称
                                                    showDialog(
                                                        context: context,
                                                        builder: (context) {
                                                          String displayName =
                                                              modelController
                                                                  .items[i]
                                                                  .displayName;
                                                          String modelName =
                                                              modelController
                                                                  .items[i]
                                                                  .name;
                                                          return AlertDialog(
                                                            titleTextStyle:
                                                                TextStyle(
                                                                    fontSize:
                                                                        18,
                                                                    color:
                                                                        textColor),
                                                            title: const Text(
                                                                '编辑模型'),
                                                            content: Column(
                                                              mainAxisSize:
                                                                  MainAxisSize
                                                                      .min,
                                                              children: [
                                                                TextField(
                                                                  decoration:
                                                                      InputDecoration(
                                                                    hintText:
                                                                        '请输入模型显示名称',
                                                                    labelText:
                                                                        '模型显示名称',
                                                                    border:
                                                                        OutlineInputBorder(
                                                                      borderSide: BorderSide(
                                                                          color:
                                                                              textColor,
                                                                          width:
                                                                              1),
                                                                      borderRadius:
                                                                          BorderRadius.circular(
                                                                              10),
                                                                    ),
                                                                  ),
                                                                  controller:
                                                                      TextEditingController(
                                                                          text:
                                                                              displayName),
                                                                  onChanged:
                                                                      (value) {
                                                                    displayName =
                                                                        value;
                                                                  },
                                                                ),
                                                                const SizedBox(
                                                                  height: 10,
                                                                ),
                                                                TextField(
                                                                  decoration:
                                                                      InputDecoration(
                                                                    hintText:
                                                                        '请输入模型名称',
                                                                    labelText:
                                                                        '模型名称',
                                                                    border:
                                                                        OutlineInputBorder(
                                                                      borderSide: BorderSide(
                                                                          color:
                                                                              textColor,
                                                                          width:
                                                                              1),
                                                                      borderRadius:
                                                                          BorderRadius.circular(
                                                                              10),
                                                                    ),
                                                                  ),
                                                                  controller:
                                                                      TextEditingController(
                                                                          text:
                                                                              modelName),
                                                                  onChanged:
                                                                      (value) {
                                                                    modelName =
                                                                        value;
                                                                  },
                                                                ),
                                                              ],
                                                            ),
                                                            actions: [
                                                              TextButton(
                                                                onPressed: () {
                                                                  Navigator.pop(
                                                                      context);
                                                                },
                                                                child: Text(
                                                                  '取消',
                                                                  style: TextStyle(
                                                                      color:
                                                                          textColor),
                                                                ),
                                                              ),
                                                              TextButton(
                                                                onPressed: () {
                                                                  // 判断DisplayName是否有重复
                                                                  for (var j =
                                                                          0;
                                                                      j <
                                                                          modelController
                                                                              .items
                                                                              .length;
                                                                      j++) {
                                                                    if (j ==
                                                                        i) {
                                                                      continue;
                                                                    }
                                                                    if (modelController
                                                                            .items[j]
                                                                            .displayName ==
                                                                        displayName) {
                                                                      // 弹出框
                                                                      showDialog(
                                                                          context:
                                                                              context,
                                                                          builder:
                                                                              (context) {
                                                                            return AlertDialog(
                                                                              title: const Text('编辑模型'),
                                                                              content: const Text('模型显示名称已存在'),
                                                                              actions: [
                                                                                TextButton(
                                                                                  onPressed: () {
                                                                                    Navigator.pop(context);
                                                                                  },
                                                                                  child: Text(
                                                                                    '好的',
                                                                                    style: TextStyle(color: textColor),
                                                                                  ),
                                                                                ),
                                                                              ],
                                                                            );
                                                                          });
                                                                      return;
                                                                    }
                                                                  }
                                                                  if (displayName
                                                                          .isNotEmpty &&
                                                                      modelName
                                                                          .isNotEmpty) {
                                                                    modelController
                                                                        .items[
                                                                            i]
                                                                        .displayName = displayName;
                                                                    modelController
                                                                        .items[
                                                                            i]
                                                                        .name = modelName;
                                                                    modelController
                                                                        .update();
                                                                    Util.writeFile(
                                                                        'models.json',
                                                                        jsonEncode(
                                                                            modelController.items));
                                                                    Navigator.pop(
                                                                        context);
                                                                  }
                                                                },
                                                                child: Text(
                                                                  '确定',
                                                                  style: TextStyle(
                                                                      color:
                                                                          textColor),
                                                                ),
                                                              ),
                                                            ],
                                                          );
                                                        });
                                                  },
                                                  icon: Icon(
                                                    Icons.edit,
                                                    color: iconColor,
                                                  ),
                                                ),
                                                IconButton(
                                                  onPressed: () {
                                                    if (modelController
                                                            .items.length ==
                                                        1) {
                                                      // 弹出框
                                                      showDialog(
                                                          context: context,
                                                          builder: (context) {
                                                            return AlertDialog(
                                                              titleTextStyle:
                                                                  TextStyle(
                                                                      fontSize:
                                                                          18,
                                                                      color:
                                                                          textColor),
                                                              title: const Text(
                                                                  '删除模型'),
                                                              content: const Text(
                                                                  '至少需要一个模型'),
                                                              actions: [
                                                                TextButton(
                                                                  onPressed:
                                                                      () {
                                                                    Navigator.pop(
                                                                        context);
                                                                  },
                                                                  child: Text(
                                                                    '好的',
                                                                    style: TextStyle(
                                                                        color:
                                                                            textColor),
                                                                  ),
                                                                ),
                                                              ],
                                                            );
                                                          });

                                                      return;
                                                    }
                                                    if (modelController
                                                            .items[i].name ==
                                                        modelController
                                                            .selectModel
                                                            .value
                                                            .name) {
                                                      // 弹出框
                                                      showDialog(
                                                          context: context,
                                                          builder: (context) {
                                                            return AlertDialog(
                                                              title: const Text(
                                                                  '删除模型'),
                                                              content: const Text(
                                                                  '选中模型不能被删除'),
                                                              actions: [
                                                                TextButton(
                                                                  onPressed:
                                                                      () {
                                                                    Navigator.pop(
                                                                        context);
                                                                  },
                                                                  child: Text(
                                                                    '好的',
                                                                    style: TextStyle(
                                                                        color:
                                                                            textColor),
                                                                  ),
                                                                ),
                                                              ],
                                                            );
                                                          });
                                                      return;
                                                    }
                                                    modelController.items
                                                        .removeAt(i);
                                                    modelController.update();
                                                    Util.writeFile(
                                                        'models.json',
                                                        jsonEncode(
                                                            modelController
                                                                .items));
                                                  },
                                                  icon: Icon(
                                                    Icons.delete,
                                                    color: iconColor,
                                                  ),
                                                ),
                                              ],
                                            )),
                                          ]),
                                      ],
                                    )),
                              ),
                              const SizedBox(
                                height: 10,
                              ),
                              SizedBox(
                                width: double.infinity,
                                child: MaterialButton(
                                  onPressed: () {
                                    // 弹出对话框，输入模型显示名称和模型名称
                                    showDialog(
                                        context: context,
                                        builder: (context) {
                                          String displayName = '';
                                          String modelName = '';
                                          return AlertDialog(
                                            titleTextStyle: TextStyle(
                                                fontSize: 18, color: textColor),
                                            title: const Text('新增模型'),
                                            content: Column(
                                              mainAxisSize: MainAxisSize.min,
                                              children: [
                                                TextField(
                                                  decoration: InputDecoration(
                                                    hintText: '请输入模型显示名称',
                                                    labelText: '模型显示名称',
                                                    border: OutlineInputBorder(
                                                      borderSide: BorderSide(
                                                          color: textColor,
                                                          width: 1),
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                              10),
                                                    ),
                                                  ),
                                                  onChanged: (value) {
                                                    displayName = value;
                                                  },
                                                ),
                                                const SizedBox(
                                                  height: 10,
                                                ),
                                                TextField(
                                                  decoration: InputDecoration(
                                                    hintText: '请输入模型名称',
                                                    labelText: '模型名称',
                                                    border: OutlineInputBorder(
                                                      borderSide: BorderSide(
                                                          color: textColor,
                                                          width: 1),
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                              10),
                                                    ),
                                                  ),
                                                  onChanged: (value) {
                                                    modelName = value;
                                                  },
                                                ),
                                              ],
                                            ),
                                            actions: [
                                              TextButton(
                                                onPressed: () {
                                                  Navigator.pop(context);
                                                },
                                                child: Text(
                                                  '取消',
                                                  style: TextStyle(
                                                      color: textColor),
                                                ),
                                              ),
                                              TextButton(
                                                onPressed: () {
                                                  // 判断DisplayName是否有重复
                                                  for (var i = 0;
                                                      i <
                                                          modelController
                                                              .items.length;
                                                      i++) {
                                                    if (modelController.items[i]
                                                            .displayName ==
                                                        displayName) {
                                                      // 弹出框
                                                      showDialog(
                                                          context: context,
                                                          builder: (context) {
                                                            return AlertDialog(
                                                              titleTextStyle:
                                                                  TextStyle(
                                                                      fontSize:
                                                                          18,
                                                                      color:
                                                                          textColor),
                                                              title: const Text(
                                                                  '新增模型'),
                                                              content: const Text(
                                                                  '模型显示名称已存在'),
                                                              actions: [
                                                                TextButton(
                                                                  onPressed:
                                                                      () {
                                                                    Navigator.pop(
                                                                        context);
                                                                  },
                                                                  child: Text(
                                                                    '好的',
                                                                    style: TextStyle(
                                                                        color:
                                                                            textColor),
                                                                  ),
                                                                ),
                                                              ],
                                                            );
                                                          });
                                                      return;
                                                    }
                                                  }
                                                  if (displayName.isNotEmpty &&
                                                      modelName.isNotEmpty) {
                                                    modelController.items.add(
                                                        Model(
                                                            displayName:
                                                                displayName,
                                                            name: modelName));
                                                    Util.writeFile(
                                                        'models.json',
                                                        jsonEncode(
                                                            modelController
                                                                .items));
                                                    Navigator.pop(context);
                                                  }
                                                },
                                                child: Text(
                                                  '确定',
                                                  style: TextStyle(
                                                      color: textColor),
                                                ),
                                              ),
                                            ],
                                          );
                                        });
                                  },
                                  color: iconColor,
                                  child: const Row(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: [
                                      Icon(
                                        Icons.add,
                                        color: Colors.white,
                                      ),
                                      Text(
                                        '新增模型',
                                        style: TextStyle(color: Colors.white),
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                      const SizedBox(
                        height: 20,
                      ),
                    ],
                  ),
                );
              } else if (selectMenu.value == '关于&帮助') {
                return Container(
                  padding: const EdgeInsets.all(20),
                  child: ListView(
                    children: [
                      const SizedBox(
                        height: 20,
                      ),
                      ListTile(
                        leading: Icon(
                          Icons.api,
                          color: iconColor,
                        ),
                        title: Text(
                          '版本',
                          style: TextStyle(color: textColor),
                        ),
                        subtitle: const Text(
                          '1.0.0',
                          style: TextStyle(color: Colors.grey),
                        ),
                      ),
                      ListTile(
                        leading: Icon(
                          Icons.info,
                          color: iconColor,
                        ),
                        title: Text(
                          '关于',
                          style: TextStyle(color: textColor),
                        ),
                        subtitle: const Text(
                          'DeepSeek Desktop 是一个基于 DeepSeek 开发的桌面客户端。',
                          style: TextStyle(color: Colors.grey),
                        ),
                      ),
                      ListTile(
                        leading: Icon(
                          Icons.help,
                          color: iconColor,
                        ),
                        title: Text(
                          '帮助',
                          style: TextStyle(color: textColor),
                        ),
                        subtitle: const Text(
                          '如果您有任何问题或发现了bug，请联系 GitHub Issues.',
                          style: TextStyle(color: Colors.grey),
                        ),
                      ),
                      ListTile(
                        leading: Icon(
                          Icons.code,
                          color: iconColor,
                        ),
                        title: Text(
                          'DeepSeek Desktop 项目代码',
                          style: TextStyle(color: textColor),
                        ),
                        subtitle: const Text(
                          ' https://github.com',
                          style: TextStyle(color: Colors.grey),
                        ),
                      ),
                      ListTile(
                        title: Row(
                          children: [
                            Icon(
                              Icons.question_answer,
                              color: iconColor,
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Text(
                              'DeepSeek Desktop 项目常见问题',
                              style: TextStyle(color: textColor),
                            )
                          ],
                        ),
                        subtitle: const Text(
                          """1. 如何使用聊天机器人？
您需要在“API 设置”中选择一个 API 提供者，然后输入访问令牌。完成这些步骤后，您就可以与聊天机器人开始交流了。
2. 如何添加聊天机器人？
点击聊天列表上方的“添加”按钮，然后选择您想要添加的聊天机器人。
3. 我是来自外国的用户，如何使用这个聊天机器人呢？
您可以使用自定义 API 提供者，并选择您选择的 API 提供者，然后输入自定义 API 基础 URL和访问令牌。
""",
                          style: TextStyle(color: Colors.grey),
                        ),
                      ),
                    ],
                  ),
                );
              }
              return Container(
                color: Colors.white,
              );
            }),
          ),
        ),
      ],
    );
  }

  Widget buildItem(String name, IconData icon) {
    return Container(
      decoration: BoxDecoration(
        color: selectMenu.value == name
            ? selectColor.withOpacity(0.3)
            : Colors.transparent,
        border: Border(
          left: BorderSide(
            color: selectMenu.value == name ? Colors.white : Colors.transparent,
            width: 3,
          ),
        ),
      ),
      child: MaterialButton(
        onPressed: () {
          selectMenu.value = name;
        },
        child: ListTile(
          title: Text(
            name,
            style: const TextStyle(color: Colors.white),
          ),
          leading: Icon(
            icon,
            color: Colors.white,
          ),
        ),
      ),
    );
  }
}
